package com.guigu.qingcheng.controller;

import com.alibaba.fastjson.JSONObject;
import com.guigu.qingcheng.client.GoodsClient;
import com.guigu.qingcheng.client.UserClient;
import com.guigu.qingcheng.pojo.TbOrder;
import com.guigu.qingcheng.pojo.TbReturnOrder;
import com.guigu.qingcheng.pojo.dto.OrderItemSku;
import com.guigu.qingcheng.pojo.vo.CloseOrderVo;
import com.guigu.qingcheng.pojo.vo.OrderHeBinVo;
import com.guigu.qingcheng.pojo.TbOrderItem;
import com.guigu.qingcheng.pojo.vo.CategoryReportVo;
import com.guigu.qingcheng.pojo.vo.SkuGoodsVo;
import com.guigu.qingcheng.service.TbOrderService;
import com.guigu.qingcheng.utils.JwtUtils;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
*
* @author Jiang
* @date 2020/07/31
*/
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "TbOrderController")
public class TbOrderController {
    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private UserClient userClient;

    @Autowired
    private GoodsClient goodsClient;
    /**
     * 统计订单
     * @return
     */
    @PostMapping("/orders")
    public R orders(@RequestBody CategoryReportVo categoryReportVo) {
        List<Map> maps = tbOrderService.orders(categoryReportVo);
        return R.ok().data("maps",maps);
    }

    @ApiOperation(value= "查询订单状态")
    @GetMapping("/Queryorderstatus/{status}")
    public R Queryorderstatus(@PathVariable("status") Integer status){
        Map orderstatus =  tbOrderService.Queryorderstatus(status);
        return R.ok().data("orderstatus",orderstatus.get("Queryorderstatus"));
    }

    @ApiOperation(value= "查询今日订单总数")
    @GetMapping("/todayOrder")
    public R todayOrder(){
        LocalDate localDate = LocalDate.now();
        Map todayOrder =  tbOrderService.findtodayOrder(localDate);
       if(todayOrder.get("findtodayOrder")==null){
           return R.ok().data("todayOrder",0);
       }
        return R.ok().data("todayOrder",todayOrder.get("findtodayOrder"));

    }



    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") String id) {

        TbOrder tbOrder = tbOrderService.findById(id);
        return R.ok().data("tbOrder",tbOrder);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbOrder>  list = tbOrderService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbOrder> pageUtils = tbOrderService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbOrder tbOrder, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbOrder> pageUtils = tbOrderService.pageQueryByAll(pageNum, pageSize, tbOrder);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbOrder tbOrder) {

        boolean b = tbOrderService.saveTbOrder(tbOrder);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbOrder tbOrder) {
        boolean b = tbOrderService.updateTbOrder(tbOrder);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        boolean b = tbOrderService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "根据ids获取信息")
    @GetMapping("/queryByIds/{ids}")
    public R queryByIds(@PathVariable("ids") String ids) {
        List<TbOrder> tbOrders = tbOrderService.pageQueryIdsByAll(ids);
        return R.ok().data("tbOrders",tbOrders);
    }

    @ApiOperation(value = "批量发货")
    @PostMapping("/batchSend")
    public R batchSend(@RequestBody List<TbOrder> tbOrders) {
        tbOrderService.batchSend(tbOrders);
        return R.ok();
    }

    @ApiOperation(value = "合并订单")
    @PostMapping("/heBin")
    public R heBin(@RequestBody OrderHeBinVo orderHeBinVo) {
        tbOrderService.heBin(orderHeBinVo);
        return R.ok();
    }

    @ApiOperation(value = "关闭订单")
    @PostMapping("/closeOrder")
    public R closeOrder(@RequestBody CloseOrderVo closeOrderVo) {
        tbOrderService.closeOrder(closeOrderVo);
        return R.ok();
    }

    @ApiOperation(value = "查询省份")
    @GetMapping("/queryChenShi")
    public R queryChenShi() {
        R r = userClient.provincesList();
        if(r.getCode()==20000){
            String s = JSONObject.toJSONString(r.getData().get("list"));
            return R.ok().data("provinces",s);
        }

        return R.error();
    }


    @ApiOperation(value = "根据省份id获取市")
    @GetMapping("/queryShiById/{id}")
    public R queryShiById(@PathVariable("id") String id) {
        R r = userClient.queryShiById(id);
        if(r.getCode()==20000){
            String s = JSONObject.toJSONString(r.getData().get("list"));
            return R.ok().data("cities",s);
        }

        return R.error();
    }

    @ApiOperation(value = "根据市id查询区列表")
    @GetMapping("/queryByQuId/{id}")
    public R queryByQuId(@PathVariable("id") String id) {
        R r = userClient.queryByQuId(id);
        if(r.getCode()==20000){
            String s = JSONObject.toJSONString(r.getData().get("list"));
            return R.ok().data("areas",s);
        }

        return R.error();
    }

    @ApiOperation(value = "根据商品id查询商品")
    @GetMapping("/queryByGoodsName/{id}")
    public R queryByGoodsName(@PathVariable("id") String id) {
        R r = goodsClient.queryByGoodsName(id);
        if(r.getCode()==20000){
            String s = JSONObject.toJSONString(r.getData().get("goodsDto"));
            return R.ok().data("goodsDto",s);
        }

        return R.error();
    }

    @ApiOperation(value = "根据商品名称查找商品  不分页")
    @GetMapping("/queryByGoodsLikeName/{name}")
    public R queryByGoodsLikeName(@PathVariable("name") String name) {
        R r = goodsClient.queryByGoodsLikeName(name);
        if(r.getCode()==20000){
            String s = JSONObject.toJSONString(r.getData().get("skus"));
            return R.ok().data("skus",s);
        }

        return R.error();
    }

    @ApiOperation(value = "根据id获取信息 只查询name")
    @GetMapping("/detailname/{id}")
    public R detailname(@PathVariable("id") Integer id) {
        R r = goodsClient.detailname(id);
        return r;
    }

    @ApiOperation(value = "取消订单")
    @PostMapping("/quxiaoDD")
    public R quxiaoDD(@RequestBody TbReturnOrder tbReturnOrder) {
        tbOrderService.quxiaoDD(tbReturnOrder);
        return R.ok();
    }

    @ApiOperation(value = "自定义方法查询 订单、订单明细、商品表 三表查询 前端我的订单数据 并分页")
    @GetMapping("/queryOrderItemSkuByUsername/{pageNum}/{pageSize}/{orderStatus}")
    public R queryOrderItemSkuByUsername(String username, @PathVariable("pageNum") Integer pageNum
            , @PathVariable("pageSize") Integer pageSize,@PathVariable("orderStatus") Integer orderStatus) {
        PageUtil<OrderItemSku> orders = tbOrderService.queryOrderItemSkuByUsername(username, pageNum, pageSize,orderStatus);
        return R.ok().data("orders", orders);
    }

    @ApiOperation(value = "通过订单Id 获取到 订单、订单明细、商品表 三表查询 的数据")
    @GetMapping("/queryOrderItemSkuByOrderId/{id}")
    public R queryOrderItemSkuByOrderId(@PathVariable String id) {
        OrderItemSku orderItemSku = tbOrderService.queryOrderItemSkuByOrderId(id);
        return R.ok().data("orderItemSku", orderItemSku);
    }


    @ApiOperation(value = "添加订单信息")
    @PostMapping("/addOrder")
    public Map<String,Object> addOrder(@RequestBody TbOrder tbOrder) {

        Map<String, Object> map = tbOrderService.addTbOrder(tbOrder);

        return map;
    }

    @ApiOperation(value = "订单确认收货 已完成订单")
    @GetMapping("/okOrder/{id}")
    public R okOrder(@PathVariable String id) {
        tbOrderService.okOrder(id);
        return R.ok();
    }



    @ApiOperation(value = "查询该sku是否存在属性")
    @PostMapping("/querySkuGoodsBySpec")
    public String querySkuGoodsBySpec(@RequestBody SkuGoodsVo skuGoodsVo) {
        String s = goodsClient.querySkuGoodsBySpec(skuGoodsVo);
        return s;
    }

}
